package nhatquang.gs.view;

import nhatquang.gs.controler.ScenesManager;
import nhatquang.gs.model.BaseScene;
import nhatquang.gs.model.Public;

import org.andengine.entity.Entity;
import org.andengine.entity.sprite.Sprite;
import org.andengine.input.touch.TouchEvent;

import android.util.Log;

public class MenuScene extends BaseScene {
	private Sprite menuackGroundSprite, mPlay, mOnSound, mOffSound;
	private Entity mEntity;

	private long T1;
	private long T2;

	@Override
	public void createScene() {
		T1 = T2 = System.currentTimeMillis();

		menuackGroundSprite = new Sprite(0, 0,
				mResourcesManager.mTextureRegionMenu, VBOM);

		mPlay = new Sprite(Public.WIDTH_SCREEN / 2
				- mResourcesManager.mTextureRegionMenu_play.getWidth() / 2,
				Public.HEIGHT_SCREEN / 2
						- mResourcesManager.mTextureRegionMenu_play.getHeight()
						/ 2, mResourcesManager.mTextureRegionMenu_play,
				mResourcesManager.VBOM) {
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
					ScenesManager.getInstance().loadMenuToLevel();
				return true;
			}
		};

		mOnSound = new Sprite(Public.WIDTH_SCREEN
				- mResourcesManager.mOnSound.getWidth() - 10, 10,
				mResourcesManager.mOnSound, VBOM) {
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {

				mResourcesManager.mMusic.pause();
				changMusic(this, mOffSound);
				return true;
			}
		};
		mOffSound = new Sprite(Public.WIDTH_SCREEN
				- mResourcesManager.mOnSound.getWidth() - 10, 10,
				mResourcesManager.mOffSound, VBOM) {
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				changMusic(this, mOnSound);
				return true;
			}
		};

		// playMusic();
		draw();
	}

	public void draw() {
		mEntity = new Entity();
		attachChild(menuackGroundSprite);
		mEntity.attachChild(mPlay);
		mEntity.attachChild(mOffSound);
		mEntity.attachChild(mOnSound);
		this.attachChild(mEntity);
		this.registerTouchArea(mOnSound);
	}

	public void playMusic() {
		// mResourcesManager.mMusic.setLooping(true);
//		mResourcesManager.mMusic.play();
	}

	public void changMusic(Sprite a, Sprite b) {
		a.setVisible(false);
		// mEntity.detachChild(a);
		// mEntity.attachChild(b);
	}

	@Override
	protected void onManagedUpdate(float pSecondsElapsed) {
		T2=System.currentTimeMillis();
		if((T2-T1)>100){
			this.registerTouchArea(mPlay);
		}
		super.onManagedUpdate(pSecondsElapsed);
	}

	@Override
	public void onBackKeyPressed() {
		this.detachChildren();
		System.exit(0);
		System.gc();
	}

}
